﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using ODS.WebService.DeviceFunctions;
using ODS.Utils;
using ODS.Model;
using ODS.WebService.DeviceMonitor;
using System.Windows.Media.Imaging;

namespace ODS
{
    public partial class SettingPage : PhoneApplicationPage
    {
        private Control currentView;
        public SettingPage()
        {
            InitializeComponent();

            currentView = SettingDeviceView;
            SettingAlertView.parrentPage = this;
            SettingDeviceView.parrentPage = this;
            ChooseLocationView.parrentPage = this;

            setValueForView();

            ChooseLocationView.OnGetGeocodingLocation += ChooseLocationView_OnGetGeocodingLocation;
            sb_fadeOut_ChooseLocationView.Completed += sb_fadeOut_popUp_Completed;
        }

        void setValueForView()
        {
            tbl_deviceName.Text = App.currentDeviceModel.VehicleName;
        }

        void sb_fadeOut_popUp_Completed(object sender, EventArgs e)
        {
            ChooseLocationView.Visibility = Visibility.Collapsed;
        }

        protected override async void OnNavigatedTo(NavigationEventArgs e)
        {
            base.OnNavigatedTo(e);

            //if navigate from ScanQRCodePage
            if (SettingDeviceView.IsScanQRCodeDeviceIMEI)
            {
                SettingDeviceView.SetSubscriptionCode(App.TextQRCode);
                SettingDeviceView.IsScanQRCodeDeviceIMEI = false;
                return;
            }

            showWaiting();
            InfoResult result_retrieveDevice = await RetrieveDeviceService.RetrieveDevice(App.currentDeviceModel.DeviceIMEI);
            if (result_retrieveDevice.IsSuccess)
            {
                if (result_retrieveDevice.ReturnedObject != null)
                {
                    DeviceModel deviceModel = result_retrieveDevice.ReturnedObject as DeviceModel;
                    if (deviceModel.DeviceIMEI.Length > 0)
                    {
                        CloneDeviceModel(deviceModel);
                    }
                }
            }

            InfoResult result_monitorService = await LoadAllDeviceMonitorsService.LoadAllDeviceMonitors(App.currentDeviceModel.DeviceIMEI);
            if (result_monitorService.IsSuccess)
            {
                if (result_monitorService.ReturnedObject != null)
                {
                    List<DeviceMonitorModel> deviceMonitorModelList = result_monitorService.ReturnedObject as List<DeviceMonitorModel>;
                    SettingAlertView.DeviceMonitorModelList = deviceMonitorModelList;
                    SettingAlertView.loadDataForView();
                }
            }

            hiddenWaiting();
        }

        protected override void OnBackKeyPress(System.ComponentModel.CancelEventArgs e)
        {
            if (ChooseLocationView.Visibility == Visibility.Visible)
            {
                e.Cancel = true;
                hiddenChooseLocationViewWithAnimation();
            }
            base.OnBackKeyPress(e);
        }

        void CloneDeviceModel(DeviceModel deviceModel)
        {
            App.currentDeviceModel.DevicePhoneNumber = deviceModel.DevicePhoneNumber;
            App.currentDeviceModel.DeviceSubscriptionCode = deviceModel.DeviceSubscriptionCode;
            App.currentDeviceModel.ExpiresAt = deviceModel.ExpiresAt;
            App.currentDeviceModel.IsActivatedSubscription = deviceModel.IsActivatedSubscription;
            App.currentDeviceModel.IsStarterOn = deviceModel.IsStarterOn;
            App.currentDeviceModel.LastAt = deviceModel.LastAt;
            App.currentDeviceModel.LastMessage = deviceModel.LastMessage;
            App.currentDeviceModel.ListAllEvents = deviceModel.ListAllEvents;
            App.currentDeviceModel.ListUnreadEvents = deviceModel.ListUnreadEvents;
            App.currentDeviceModel.RemainingRequestNumber = deviceModel.RemainingRequestNumber;
            App.currentDeviceModel.TitleRequest = deviceModel.TitleRequest;
            App.currentDeviceModel.UnreadNumber = deviceModel.UnreadNumber;
            App.currentDeviceModel.VehicleName = deviceModel.VehicleName;
        }

        private void showView(Control view)
        {
            currentView.Visibility = Visibility.Collapsed;
            view.Visibility = Visibility.Visible;

            currentView = view;
        }

        public void showChooseLocationViewWithAnimation()
        {
            ChooseLocationView.Visibility = Visibility.Visible;
            ChooseLocationView.Opacity = 0;
            sb_fadeIn_ChooseLocationView.Begin();
            ChooseLocationView.getMyLocation();
        }

        public void hiddenChooseLocationViewWithAnimation()
        {
            sb_fadeOut_ChooseLocationView.Begin();
        }



        private void btn_settingAlert_click(object sender, System.Windows.RoutedEventArgs e)
        {
            if (currentView != SettingAlertView)
            {
                showView(SettingAlertView);

                img_settingDevice.Source = new BitmapImage(new Uri("/Assets/images/device icon.png", UriKind.Relative));
                img_settingAlert.Source = new BitmapImage(new Uri("/Assets/images/alerts icon selecte.png", UriKind.Relative));
            }
        }

        private void btn_settingDevice_click(object sender, System.Windows.RoutedEventArgs e)
        {
            if (currentView != SettingDeviceView)
            {
                showView(SettingDeviceView);

                img_settingDevice.Source = new BitmapImage(new Uri("/Assets/images/device icon (selected).png", UriKind.Relative));
                img_settingAlert.Source = new BitmapImage(new Uri("/Assets/images/alerts icon.png", UriKind.Relative));
            }
            

        }

        private async void btnSet_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            btnSet.IsEnabled = false;
            showWaiting();
            if (currentView == SettingDeviceView)
            {
                await SettingDeviceView.Apply();
            }
            else
            {
                await SettingAlertView.Apply();
            }
            hiddenWaiting();
            btnSet.IsEnabled = true;
        }

        void showWaiting()
        {
            progressBar_waiting.Visibility = Visibility.Visible;
            btnSet.IsEnabled = false;
        }

        void hiddenWaiting()
        {
            progressBar_waiting.Visibility = Visibility.Collapsed;
            btnSet.IsEnabled = true;
        }


        private void btnBack_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            this.NavigationService.GoBack();
        }

        void ChooseLocationView_OnGetGeocodingLocation(Place place)
        {
            SettingAlertView.setLocation(place);
        }
    }
}